package org.lboot.rabbit.test;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lboot.rabbit.client.RocketRabbitAdminClient;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("mq/admin")
@Api(tags = "MQ管理")
@AllArgsConstructor
public class MqAdminController {
    RocketRabbitAdminClient adminClient;

    RabbitProperties props;

    @PostMapping("queues")
    @ApiOperation(value = "队列新建")
    public String declareQueue(@RequestParam("queueName") String queueName){
        adminClient.addQueue(queueName);
        return "ok";
    }

    @GetMapping("queues")
    @ApiOperation(value = "队列列表")
    public Object listQueues(){
        String url = "150.158.135.236:15672/api/queues";
        String result = HttpRequest.get(url)
                .basicAuth(props.getUsername(), props.getPassword()) // 设置基本认证
                .execute().body();
        return JSONUtil.parse(result);
    }
}
